package com.hadoop.dao.demo;

import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.junit.Test;

import com.google.common.collect.Lists;
import com.hadoop.dao.HdfsDao;
import com.hadoop.dao.exception.HdfsDaoException;
import com.hadoop.dao.util.CheckUtil;

public class HdfsDaoDemo {
	private static final String HDFS = "hdfs://192.168.252.128:9000/";

	@Test
	public void mkdir() throws HdfsDaoException {
		HdfsDao hdfsDao = new HdfsDao(HDFS, new Configuration());
		System.out.println(hdfsDao.mkdir("/test"));
	}

	@Test
	public void rmr() throws HdfsDaoException {
		HdfsDao hdfsDao = new HdfsDao(HDFS, new Configuration());
		System.out.println(hdfsDao.rmr("/test"));
	}

	@Test
	public void ls() throws HdfsDaoException {
		HdfsDao hdfsDao = new HdfsDao(HDFS, new Configuration());
		int count = 0;
		List<Path> pathList = hdfsDao.ls("/test");
		if (CheckUtil.isNotEmpty(pathList)) {
			for (Path path : pathList) {
				System.out.println(path);
				++count;
			}
		}
		System.out.println("count = " + count);
	}

	@Test
	public void create() throws HdfsDaoException {
		HdfsDao hdfsDao = new HdfsDao(HDFS, new Configuration());
		List<String> contentList = Lists.newArrayList();
		for (int i = 1; i <= 100; ++i) {
			contentList.add(i + "");
		}
		System.out.println(hdfsDao.create("/test/test.txt", contentList));
	}

	@Test
	public void upload() throws HdfsDaoException {
		HdfsDao hdfsDao = new HdfsDao(HDFS, new Configuration());
		System.out.println(hdfsDao.upload("D:\\HDTunePro.exe", "/test"));
	}

	@Test
	public void download() throws HdfsDaoException {
		HdfsDao hdfsDao = new HdfsDao(HDFS, new Configuration());
		System.out.println(hdfsDao.download("/test/test.txt", "D:"));
	}

}